﻿Public Class perCliente
    Inherits perAcessoBD

    ReadOnly Property sqlConsulta() As String
        Get
            Dim sSql As String
            sSql = "SELECT CodCliente as Código, razaoSocial as [RazãoSocial], apelido as Apelido, CNPJ"
            sSql &= " FROM Cliente"
            sSql &= " ORDER BY razaoSocial"
            Return sSql
        End Get
    End Property

    Public Function selecionarCliente(ByVal sCodCliente As String) As DataTable
        Dim sSql As String
        Dim datRetorno As New DataTable

        Try
            sSql = "SELECT razaoSocial " &
                   "  FROM cliente " &
                   " WHERE RTrim(LTrim(codCliente)) = @codCliente"

            With MyBase.SQLCmd.Parameters
                .Clear()
                .AddWithValue("@codCliente", sCodCliente.Trim)
            End With

            datRetorno = MyBase.executarConsulta(sSql)

            If datRetorno.Rows.Count > 0 Then
                Return datRetorno
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw New Exception("Ocorreu um erro ao selecionar os dados do cliente." & Environment.NewLine & ex.Message)
        End Try

    End Function

    Public Function inserirCliente(ByVal sNomeCliente As String) As Int32
        Dim sSql As String
        Dim iCodigo As Int32 = 0



        sSql = "INSERT INTO Cliente " & Environment.NewLine &
               "    (" & Environment.NewLine &
               "    Nome" & Environment.NewLine &
               "    )" & Environment.NewLine &
               " VALUES " & Environment.NewLine &
               "    (" & Environment.NewLine &
               "    @Nome" & Environment.NewLine &
               "    )" & Environment.NewLine

        With MyBase.SQLCmd.Parameters
            .Clear()
            .AddWithValue("@Nome", sNomeCliente.Trim)
        End With

        MyBase.executarAcao(sSql)

    End Function

End Class
